﻿using Student.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Student
{
    public partial class DaKa : Form
    {
        public DaKa()
        {
            InitializeComponent();
            timer1_Tick(null,null);  // 首次调用定时器函数 传递null 第一次执行的延迟时间取消掉
            timer1.Interval = 1000;
            timer1.Start();
            this.dataGridView2.AutoGenerateColumns = false;//禁止自动生成列表题

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }
        // 定时器方法 默认每隔100ms执行1次
        private void timer1_Tick(object sender, EventArgs e)
        {
            // 处理时间
            DateTime d1 = DateTime.Now; 
            this.label2.Text = d1.Year.ToString(); // 年
            this.label3.Text = d1.Month.ToString();// 月
            this.label4.Text = d1.Day.ToString(); //日
            this.label5.Text = d1.ToLongTimeString();  // 时分秒

            switch (d1.DayOfWeek) // 
            {
                case DayOfWeek.Sunday:
                    this.label6.Text = "日";
                    break;
                case DayOfWeek.Monday:
                    this.label6.Text = "一";
                    break;
                case DayOfWeek.Tuesday:
                    this.label6.Text = "二";
                    break;
                case DayOfWeek.Wednesday:
                    this.label6.Text = "三";
                    break;
                case DayOfWeek.Thursday:
                    this.label6.Text = "四";
                    break;
                case DayOfWeek.Friday:
                    this.label6.Text = "五";
                    break;
                case DayOfWeek.Saturday:
                    this.label6.Text = "六";
                    break;
                default:
                    break;
            }
        }
        public List<DaKa1> listDaKa= new List<DaKa1>();    //存放打卡信息
       // 卡号回车事件
        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13) // 是否按了回车键
            {
                if (this.textBox1.Text.Length == 0)
                {
                    MessageBox.Show("输入的卡号不能为空");
                    return;
                }
                // 先从本地把学生数据取出来
                List<Student1> big = null; // 学生信息
                try
                {
                    big = Student.Tools.SaveData.QuData("Students.txt") as List<Student1>;
                }
                catch (Exception)
                {
                    big = new List<Student1>(); // 第一次存储存学生执行
                }

                // 根据卡号找学生
                Student1 stu = null;  // 查找的学生对象
                foreach (Student1 s  in big)
                {
                    if(this.textBox1.Text == s.StudentCarNum) //当前卡号和数组有一个卡号一致 找到对应对象 赋值给stu
                    {
                        //找到对应学生对象
                        stu = s;
                        break;
                    }

                }
                if (stu != null) //找到学生对象
                {
                    this.label18.Text = "打卡成功";
                    this.label12.Text = stu.StudentName;
                    this.label14.Text = stu.StudentId.ToString();
                    this.label16.Text = stu.StudentClassName;

                    DaKa1 dk = new DaKa1(); //创建一个打卡记录对象 每一个属性赋值
                    dk.DTime = DateTime.Now;  
                    dk.StudentId = stu.StudentId;
                    dk.StudentName = stu.StudentName;
                    dk.StudentCardNum = stu.StudentCarNum;
                    dk.Gender = stu.Gender;
                    dk.ClassName = stu.StudentClassName;
                    listDaKa.Add(dk);
                    this.dataGridView2.DataSource = null;
                    this.dataGridView2.DataSource = listDaKa;
                }
            }
        }

        // 晚上添加的任务：把打卡记录在关闭窗口的时候进行本地存储 在打卡查询界面进行本地取出并且展示数据
        // 切换账号一块 模型类 包含账号和密码 User u1 = new User() u1.zhanghao= u1.mima u1.nicknam=
        // 修改密码
        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        // 存储打卡记录
        private void DaKa_FormClosing(object sender, FormClosingEventArgs e)
        {
            List<DaKa1> list = null;
            try
            {
                //先从本地取出打卡记录
                list = Student.Tools.SaveData.QuData("Daka.txt") as List<DaKa1>;
            }catch(Exception ex)
            {
                list = new List<DaKa1>(); //第一次打卡
            }

            // 把本次的打卡记录和之前打卡合并一起储存
            list.AddRange(listDaKa);
            Student.Tools.SaveData.Save("Daka.txt",list);

        }
    }
}
